package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignModuleVo;
import com.guigu.erp.vo.StorageCellVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@RequestMapping("/storage/cell")
@Controller
public class StorageCellController {
    @Autowired
    private StorageCellService storageCellService;

    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private DesignFileService designFileService;

    /**
     * 制定安全配置单
     * @param storageCell
     * @return
     */
    @RequestMapping("addSecurityConfiguration")
    @ResponseBody
    public boolean addSecurityConfiguration(StorageCell storageCell){
        //得到产品编号
        String productId = storageCell.getProductId();
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<DesignFile>();
        wrapper.eq("PRODUCT_ID",productId);
        DesignFile designFile = designFileService.getOne(wrapper);
        String typeNumber = numberUtils.getTypeNumber("400");
        //设置产品各级分类
        storageCell.setFirstKindId(designFile.getFirstKindId());
        storageCell.setFirstKindName(designFile.getFirstKindName());
        storageCell.setSecondKindId(designFile.getSecondKindId());
        storageCell.setSecondKindName(designFile.getSecondKindName());
        storageCell.setThirdKindId(designFile.getThirdKindId());
        storageCell.setThirdKindName(designFile.getThirdKindName());
        //设置库存编号
        storageCell.setStoreId(typeNumber);
        //设置当前存储量
        storageCell.setAmount(new BigDecimal(0));
        //设置审核标志
        storageCell.setCheckTag("S001-0");
        //修改产品表的库存标志
        designFile.setDesignCellTag("K001-1");
        designFileService.updateById(designFile);
        boolean falg = storageCellService.save(storageCell);
        return falg;
    }

    /**
     * 查询未审核的安全配置单
     * @return
     */
    @RequestMapping("inventoryNotApproved")
    @ResponseBody
    public PageUtils<StorageCell> inventoryNotApproved(Integer pageNo,Integer pageSize){
        QueryWrapper<StorageCell> wrapper = new QueryWrapper<>();
        wrapper.eq("CHECK_TAG","S001-0");
        PageHelper.startPage(pageNo, pageSize);
        List<StorageCell> cellList = storageCellService.list(wrapper);
        PageInfo<StorageCell> pageInfo = new PageInfo<>(cellList);
        PageUtils<StorageCell> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    /**
     * 根据id查询安全配置单
     */
    @RequestMapping("getIdSecurityConfiguration")
    @ResponseBody
    public StorageCell getIdSecurityConfiguration(Integer id){
        StorageCell storageCell = storageCellService.getById(id);
        return storageCell;
    }

    /**
     * 点击安全配置复核，是否复核通过
     */
    @RequestMapping("securityConfigurationReview")
    @ResponseBody
    public boolean securityConfigurationReview(StorageCell storageCell){
        storageCell.setCheckTag("S001-1");
        boolean flag = storageCellService.updateById(storageCell);
        return flag;
    }

    /**
     * 安全配置单查询
     */
    @RequestMapping("safetyStockInquiry")
    @ResponseBody
    public PageUtils<StorageCell> safetyStockInquiry(DesignModuleVo vo,Integer pageNo,Integer pageSize){
        PageUtils<StorageCell> safetyStock = storageCellService.getAllSafetyStock(vo, pageNo, pageSize);
        return safetyStock;
    }

    /**
     * 动态查询
     */
    @RequestMapping("dynamicQueriesByStorageCell")
    @ResponseBody
    public PageUtils<StorageCell> dynamicQueriesByStorageCell(StorageCellVo vo,Integer pageNo,Integer pageSize){
        PageUtils<StorageCell> dynamicQueries = storageCellService.dynamicQueries(vo, pageNo, pageSize);
        return dynamicQueries;
    }
}
